<template>
  <div class="WorkingSchedule" v-loading="loading">
    <el-button type="primary" size="default" @click="onPostModel"
      >新增职位</el-button
    >

    <el-form ref="form" :model="form" label-position="top" class="myform">
      <div>
        <el-form-item label="职位名称" class="myform-item">
          <el-input
            v-model="form.title"
            placeholder="请输入职位名称"
            @keyup.enter.native="onSearch"
            clearable
          ></el-input>
        </el-form-item>
        <el-form-item label="部门" class="myform-item">
          <el-cascader
            v-model="postIds"
            :options="postlist"
            @change="handleChangeSearch"
            clearable
            :props="Props"
          ></el-cascader>
        </el-form-item>
        <el-form-item style="margin-top: 50px">
          <el-button type="primary" @click="onSearch">查询</el-button>
        </el-form-item>
      </div>

      <div>
        <el-form-item style="margin-top: 50px">
          <el-button type="primary" @click="onOn" style="margin:0 10px">一键开启DISC</el-button>
        </el-form-item>
        <el-form-item style="margin-top: 50px">
          <el-button type="danger" @click="onOff">一键关闭DISC</el-button>
        </el-form-item>
      </div>
    </el-form>
    <el-table :data="tableData" style="width: 100%" border>
      <el-table-column label="职位" align="center" prop="title">
      </el-table-column>
      <el-table-column
        label="部门"
        align="center"
        prop="factory_name_jiancheng"
      >
        <template slot-scope="{ row }">
          {{ row.t_title }}
        </template>
      </el-table-column>
      <el-table-column label="DISC" align="center" prop="sdic_status">
        <template slot-scope="{ row }">
          {{ row.sdic_status == 0 ? "开启" : "未开启" }}
        </template>
      </el-table-column>

      <el-table-column label="操作" align="center">
        <template slot-scope="{ row }">
          <el-button type="text" size="default" @click="onEdit(row)"
            >编辑</el-button
          >
          <el-button type="text" size="default" @click="onAddPositionIssue(row)"
            >添加岗位问题</el-button
          >
          <el-button
            type="text"
            size="default"
            style="color: red"
            @click="onDelete(row.id)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <pagination
      :form="form"
      :total="total"
      @paginationPostOrderTaskList="
        postAdminRecruitmentInformationSelectPosition
      "
    />

    <el-dialog
      title="添加职位"
      :visible.sync="dialogVisible"
      :close-on-click-modal="false"
      width="30%"
    >
      <el-form
        ref="userForm"
        :model="userForm"
        :rules="rules"
        label-width="120px"
      >
        <el-form-item label="部门" class="myform-item" prop="t_id">
          <el-cascader
            v-model="postIdsSearch"
            :options="postlist"
            @change="handleChange"
            :props="Props"
          ></el-cascader>
        </el-form-item>
        <el-form-item label="职位名称" class="myform-item" prop="title">
          <el-input
            v-model="userForm.title"
            placeholder="请输入职位名称"
            style="width: 300px"
          ></el-input>
        </el-form-item>
        <el-form-item label="DISC" class="myform-item" prop="sdic_status">
          <el-form-item>
            <el-select v-model="userForm.sdic_status" placeholder="请选择">
              <el-option
                v-for="item in options"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              >
              </el-option>
            </el-select>
          </el-form-item>
        </el-form-item>
        <el-form-item>
          <el-button
            type="primary"
            style="width: 70%; margin: 30px 0"
            @click="onAddPostPup('userForm')"
            :loading="loading"
            >添加</el-button
          >
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
import {
  postAdminRecruitmentInformationSelectPosition,
  postAdminRecruitmentInformationGetProfession,
  postAdminRecruitmentInformationaddPosition,
  postAdminRecruitmentInformationdelPosition,
  postAdminRecruitmentInformationOnekeyPositionDisc,
} from "@/api";
import pagination from "@/components/pagination.vue";
export default {
  components: { pagination },
  data() {
    return {
      loading: false,
      dialogVisible: false,
      postIds: "",
      postIdsSearch: "",
      Props: {
        value: "id",
        label: "title",
        children: "position",
        checkStrictly: true,
      },
      postlist: [],
      form: {
        t_id: "",
        title: "",
        page: 1,
        size: 10,
      },
      userForm: {
        t_id: "",
        title: "",
        sdic_status: "",
      },
      rules: {
        t_id: [{ required: true, message: "请选择部门", trigger: "blur" }],
        title: [{ required: true, message: "请输入岗位名称", trigger: "blur" }],
        sdic_status: [
          { required: true, message: "请选择是否开启DISC", trigger: "blur" },
        ],
      },
      options: [
        {
          label: "开启",
          value: "0",
        },
        {
          label: "关闭",
          value: "1",
        },
      ],
      tableData: [],
      total: 1,
    };
  },
  activated() {
    let form = this.form;
    this.postAdminRecruitmentInformationSelectPosition(form);
    this.postAdminRecruitmentInformationGetProfession();
  },
  methods: {
    async onOn() {
      let obj = {
        sdic_status: 1,
      };
      await postAdminRecruitmentInformationOnekeyPositionDisc(obj);
      let form = this.form;
      this.postAdminRecruitmentInformationSelectPosition(form);
      this.postAdminRecruitmentInformationGetProfession();
    },
    async onOff() {
      let obj = {
        sdic_status: 0,
      };
      await postAdminRecruitmentInformationOnekeyPositionDisc(obj);
      let form = this.form;
      this.postAdminRecruitmentInformationSelectPosition(form);
      this.postAdminRecruitmentInformationGetProfession();
    },
    // 编辑
    onEdit(row) {
      this.postIds = "";
      this.userForm = {
        id: row.id,
        t_id: "",
        title: row.title,
        sdic_status: "",
      };
      this.dialogVisible = true;
    },
    onDelete(id) {
      let that = this;
      let obj = {
        id,
      };
      this.$confirm("确定删除该职位?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          postAdminRecruitmentInformationdelPosition(obj).then((res) => {
            let form = that.form;
            this.postAdminRecruitmentInformationSelectPosition(form);
            this.$message({
              type: "success",
              message: "删除成功!",
            });
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    handleChangeSearch(e) {
      console.log("handleChangeSearch------->", e);
      let arr = JSON.parse(JSON.stringify(e));
      this.form.t_id = arr.pop();
    },
    handleChange(e) {
      console.log("e------->", e);
      let arr = JSON.parse(JSON.stringify(e));
      this.userForm.t_id = arr.pop();
    },
    onPostModel() {
      this.postIds = "";
      this.userForm = {
        t_id: "",
        title: "",
        sdic_status: "",
      };
      this.dialogVisible = true;
    },
    // 部门
    postAdminRecruitmentInformationGetProfession() {
      postAdminRecruitmentInformationGetProfession().then((res) => {
        this.postlist = res.data;
      });
    },
    onAddPositionIssue(e) {
      let detail = JSON.stringify(e);
      this.$router.push({ name: "HRInterviewIssue", query: { detail } });
    },
    // 采购列表
    async postAdminRecruitmentInformationSelectPosition(data) {
      this.loading = true;
      this.form = data;
      postAdminRecruitmentInformationSelectPosition(data)
        .then((res) => {
          this.loading = false;
          console.log("采购列表", res);
          this.tableData = res.data;
          this.total = Number(res.count);
        })
        .catch((err) => {
          this.loading = false;
        });
    },
    // 添加岗位
    onAddPostPup(formName) {
      let that = this;
      this.$refs[formName].validate((valid, data) => {
        if (valid) {
          console.log("data-------->", data);
          this.loading = true;
          postAdminRecruitmentInformationaddPosition(that.userForm).then(
            (res) => {
              let form = that.form;
              this.postAdminRecruitmentInformationSelectPosition(form);
              this.dialogVisible = false;
              this.loading = false;
            }
          );
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    // 搜索
    onSearch() {
      this.form.page = 1;
      this.form.size = 10;
      console.log("this.form", this.form);
      this.postAdminRecruitmentInformationSelectPosition(this.form);
    },
  },
};
</script>

<style lang="scss" scoped>
.myform {
  display: flex;
  justify-content: space-between;
  > div {
    display: flex;
  }
  &-item {
    margin-right: 10px;
  }
}
.WorkingSchedule {
  background: #ffff;
  padding: 20px;
  border-radius: 2px;
  @include boxShadow;
}
</style>
